//
//  PersonCertV2ViewModel.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2018/11/8.
//  Copyright © 2018年 HCreater. All rights reserved.
//

#import "PersonCertV2ViewModel.h"

@interface PersonCertV2ViewModel ()

/// 验证码按钮点击执行的命令
@property (nonatomic, readwrite, strong) RACCommand *verCodeCommand;
/// 提交按钮点击执行的命令
@property (nonatomic, readwrite, strong) RACCommand *submitCommand;

@end

@implementation PersonCertV2ViewModel

#pragma mark - Ovrride
- (void)initialize
{
    [super initialize];
    
    @weakify(self)
    
    self.isBack2Dismiss = NO;
    
    /// 验证码
    self.verCodeCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input)
    {
        @strongify(self);
           
        DataManager* dm = [DataManager sharedDataManager];
           
        if (dm.phoneVCodeCaler.verCodeTime > 0)
        {
            return [RACSignal error:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:[NSString stringWithFormat:@"请%d秒后再试", dm.phoneVCodeCaler.verCodeTime]}]];
        }
           
        if (![NSString mh_isValidMobile:self.strPhone]) {
               
            return [RACSignal error:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:@"请输入正确的手机号码"}]];
        }
           
        return [self requestVercodeSignal];
    }];
    
    /// 注册命令
    self.submitCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self);
        
        if (![NSString mh_isValidMobile:self.strPhone])
        {
            return [RACSignal error:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:@"请输入正确的手机号码"}]];
        }
        if (![NSString mh_isValidCharacterOrNumber:self.strPwd] || self.strPwd.length < 6 || self.strPwd.length > 12) {
            
            return [RACSignal error:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:@"密码填写错误"}]];
        }
        if (![NSString mh_isPureDigitCharacters:self.strVercode] || self.strVercode.length != 6 ) {
            
            return [RACSignal error:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:@"验证码填写错误"}]];
        }
        if (![self.contrastVerCode isEqualToString:self.strVercode])
        {
            return [RACSignal error:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:@"验证码错误"}]];
        }
        
        return [self requestPersonCertSignal];
    }];
}

#pragma mark - Request
- (RACSignal*)requestVercodeSignal
{
    @weakify(self)
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        @strongify(self)
        
        DataManager* dm = [DataManager sharedDataManager];
        [dm.phoneVCodeCaler waitGetCode];
        
        @weakify(self)
        /// 发起请求
        [[AFNetWorkUtils racPOSTWthURL:url_interface_sendMessage params:interface_param_sendMessage(self.strPhone)] subscribeNext:^(id x)
         {
             //成功返回(result class is NSDictionary)
             
             @strongify(self);
             
             self.contrastVerCode = x[@"code"];
             
             [subscriber sendNext:x[SUCommandSysMessageKey]];
             [subscriber sendCompleted];
         } error:^(NSError *error) {
             //错误处理
             
             NSString* strError = [error.userInfo[customErrorInfoKey] isNotBlank] ? error.userInfo[customErrorInfoKey] : @"请求验证码失败，请稍后再试";
             [subscriber sendError:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:strError}]];
             [subscriber sendCompleted];
         }];
        
        return nil;
    }];
}

- (RACSignal*)requestPersonCertSignal
{
    UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
    
    @weakify(self)
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        @strongify(self)
        
        @weakify(self)
        /// 发起请求
        [[AFNetWorkUtils racPOSTWthURL:url_interface_bindingmobile params:interface_param_bindingmobile(um.userData.userID, self.strPhone, self.strPwd)] subscribeNext:^(id x)
         {
             @strongify(self)
             
             [subscriber sendNext:x[SUCommandSysMessageKey]];
             [subscriber sendCompleted];
         }   error:^(NSError *error) {
             //错误处理
             
             NSString* strError = [error.userInfo[customErrorInfoKey] isNotBlank] ? error.userInfo[customErrorInfoKey] : @"个人认证失败";
             [subscriber sendError:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:strError}]];
             [subscriber sendCompleted];
         }];
        
        return nil;
    }];
}

@end
